
package com.nd.rj.common.login;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.encodings.PKCS1Encoding;
import org.bouncycastle.crypto.engines.RSAEngine;
import org.bouncycastle.crypto.params.RSAKeyParameters;
import org.bouncycastle.util.encoders.BASE64Encoder;

import android.text.InputFilter;
import android.text.Spanned;
import android.widget.EditText;

public class NdLoginComFun {
    public static void SetEditTextEditable(EditText editText, Boolean editable) {
        if (editable) {
            editText.setFocusableInTouchMode(true);
            editText.setFilters(new InputFilter[] {
                new InputFilter() {
                    @Override
                    public CharSequence filter(CharSequence source, int start, int end,
                            Spanned dest, int dstart, int dend) {
                        // TODO Auto-generated method stub
                        return null;
                    }
                }
            });

        } else {
            editText.setFocusableInTouchMode(false);
            editText.setFilters(new InputFilter[] {
                new InputFilter() {
                    @Override
                    public CharSequence filter(CharSequence source, int start, int end,
                            Spanned dest, int dstart, int dend) {
                        return source.length() < 1 ? dest.subSequence(dstart, dend) : "";
                    }
                }
            });
        }
    }

    /**
     * md5加密函数
     * 
     * @param aSrc 被加密串
     * @return
     */
    public static String Md5Digest(String aSrc, boolean aUpperCase) {
        String sResult = "";
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(aSrc.getBytes());
            byte b[] = md5.digest();
            StringBuffer buf = new StringBuffer("");
            // 转16进制
            for (int offset = 0; offset < b.length; offset++) {
                int i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            sResult = buf.toString();
            if (aUpperCase) {
                sResult = sResult.toUpperCase();
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return sResult;
    }

    public static String Md5Digest(String aSrc) {
        return Md5Digest(aSrc, true);
    }
    
    /**
     * RSA加密算法
     * 
     * @param cleartext明文
     * @param modul
     * @param pubExp
     * @return密文
     */
    public static String RSAEncrypt(String cleartext, String modul, String pubExp) {
        String sresult = "";
        BigInteger bmodul = new BigInteger(modul, 16);
        BigInteger bpubExp = new BigInteger(pubExp, 16);
        RSAKeyParameters pubkey = new RSAKeyParameters(false, bmodul, bpubExp);
        org.bouncycastle.crypto.AsymmetricBlockCipher eng = new PKCS1Encoding(new RSAEngine());
        eng.init(true, pubkey);
        byte[] data = null;
        try {
            data = eng.processBlock(cleartext.getBytes(), 0, cleartext.getBytes().length);
            BASE64Encoder encode = new BASE64Encoder(data);
            sresult = new String(encode.encode());
        } catch (InvalidCipherTextException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sresult;

    }
    
    /**
     *  判断输入字符是否是数字
      * @Title: isNumeric
      * @Description: TODO
      * @author chenyw
      * @date 2012-8-2
      * @param @param str
      * @param @return
      * @return boolean
      * @throws
     */
    public static boolean isNumeric(String str){ 
        Pattern pattern = Pattern.compile("[0-9]*"); 
        return pattern.matcher(str).matches(); 
    } 
    
    /**
     *  判断输入字符是否是手机号码
      * @Title: isMobileNO
      * @Description: TODO
      * @author chenyw
      * @date 2012-8-2
      * @param @param mobiles
      * @param @return
      * @return boolean
      * @throws
     */
    public static boolean isMobileNO(String mobiles){

      Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");

      Matcher m = p.matcher(mobiles);

      return m.matches();

    }
}
